var app = getApp();

Page({
  data: {},
  onLoad: function(e) {
    var that = this;
    that.setData({
      navH: app.globalData.navHeight,
    })


  },
  onGotUserInfo(e) {
    var that = this;
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("登录", res)
        var getCode = res.code; //用户授权码
        if (res.code) {
          //用户注册
          wx.getSetting({
            success: res => {
              console.log("注册", res)
              if (res.authSetting['scope.userInfo']) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                wx.getUserInfo({
                  success: res => {
                    console.log("用户信息", res)
                    var getEncryptedData = res.encryptedData;
                    var getIv = res.iv;
                    var getRawData = res.rawData;
                    //注册用户信息到内部用户表
                    wx.request({
                      url: app.globalData.apiHost + "/api/user/third",
                      data: {
                        platform: 'wechatmini',
                        code: getCode,
                        encryptedData: getEncryptedData,
                        iv: getIv,
                        rawData: getRawData
                      },
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      method: "POST",
                      dataType: "json",
                      success: function(data) {
                        if (data.statusCode === 200) {
                          var logToken = data.data.data.userinfo.token;
                          //把token存入缓存
                          wx.setStorageSync('token', logToken);
                          let pages = getCurrentPages();
                          let prevpage = pages[pages.length - 2];
                          if (prevpage.route == "pages/my/index/my-index") {
                            wx.switchTab({
                              url: '/pages/index/index',
                            })
                          } else {
                            wx.navigateBack({
                              delta: 1,
                            }); //返回上一页面
                          }
                          // wx.reLaunch({
                          //   url: '/pages/index/index'
                          // })
                        }
                      },
                    });
                    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                    // 所以此处加入 callback 以防止这种情况
                    if (this.userInfoReadyCallback) {
                      this.userInfoReadyCallback(res)
                    }
                  }
                });
              }
            },
            fail: function(e) {
              alert(e);
            },
            complete: function(obj) {}
          });
        } else {
          console.log("授权失败" + res.errMsg);
        }
      }
    })
  },
  toBack: function() {
    let pages = getCurrentPages();
    let prevpage = pages[pages.length - 2];
    if (prevpage.route == "pages/my/index/my-index") {
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      wx.navigateBack({
        delta: 2,
      }); //返回上一页面
    }



  }

})